#include "menu.h"
#include <allegro.h>
#include <vector>
#include <stdio.h>
using namespace std;

menu::menu(char *file) {
	this->selected = 0;
	this->seta = load_bitmap("media/seta.bmp", NULL);
	char bmp[150];
	char temp_text[150];
	int temp_w, temp_h;
	int temp_x, temp_y;
	FILE *pmenu = fopen(file, "r");
	fscanf(pmenu,"%s", bmp);
	fscanf(pmenu,"%d %d", &temp_w, &temp_h);
	while (!feof(pmenu)) {
		fscanf(pmenu, "%d %d %s", &temp_x, &temp_y, temp_text);
		if(feof(pmenu))
			break;
		this->options.push_back(new option(temp_x, temp_y, temp_text));
	}
	this->w = temp_w;
	this->h = temp_h;
	this->background = load_bmp(bmp, NULL);
}

void menu::draw(BITMAP *buffer) {
	if(this->w == 0 && this->h == 0) {
		masked_stretch_blit(this->background, buffer, 0, 0, this->background->w, this->background->h, 0, 0, TELA_W, TELA_H);
		if (options.size() == 0)
			return;
		for(int i = 0; i < options.size(); i++)
			options[i]->draw(buffer);
	}
	else {
		masked_stretch_blit(this->background, buffer, 0, 0, this->background->w, this->background->h, (TELA_W - this->w)/2, (TELA_H - this->h)/2, this->w, this->h);
		if (options.size() == 0)
			return;
		for(int i = 0; i < options.size(); i++)
			options[i]->draw(buffer);
	}
	draw_sprite(buffer, seta, (options[selected]->x - seta->w - 5), (options[selected]->y - seta->h/2));
}

void menu::move(int direction) {
	if (direction == 1) {
		if(selected == (options.size() - 1))
			selected = 0;
		else
			selected++;
	}
	else if (direction == 0) {
		if(selected == 0)
			selected = options.size() - 1;
		else
			selected--;
	}
}

